package server.business.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import server.business.pojo.BloodTransfer;
import server.business.pojo.BloodTransferInformation;
import server.business.service.BloodTransferDetailService;
import server.business.service.BloodTransferInformationService;
import server.business.service.BloodTransferService;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.results.ysl.Result;

import java.util.List;

@Slf4j
@RequestMapping("/bloodtransfer")
@RestController
public class BloodTransferController {

    @Autowired
    private BloodTransferService bloodTransferService;
    @Autowired
    private BloodTransferDetailService bloodTransferDetailService;
    @Autowired
    private BloodTransferInformationService bloodTransferInformationService;


    /**
     *
     * @param string 调血申请单号
     * @return 符合条件的血液信息
     */
    @GetMapping("/normal/distribute")
    public Result distribute(String string){
        List<BloodTransferInformation> bloodTransferInformations = bloodTransferService.distribute1(string);
        return Result.success(bloodTransferInformations);
    }

    /**
     * 分页查询
     * @param currentPage
     * @param pageSize
     * @param bloodTransfer
     * @return
     */
    @GetMapping("/normal/{currentPage}/{pageSize}")
    public Result getPage(@PathVariable int currentPage, @PathVariable int pageSize,
                          BloodTransfer bloodTransfer){
        IPage<BloodTransfer> page = bloodTransferService.getPage(currentPage,pageSize,bloodTransfer);
        if (currentPage > page.getPages()) {
            page = bloodTransferService.getPage((int) page.getPages(), pageSize, bloodTransfer);
        }
        return Result.success(page);
    }

    @GetMapping(value = "/review/setState")
    public Result setStateByNumber(BloodTransfer bloodTransfer){
        bloodTransferService.setStateByNumber(bloodTransfer);
        return Result.success("审核通过");
    }
    @GetMapping(value = "/review/setState1")
    public Result setStateByNumber1(BloodTransfer bloodTransfer){
        bloodTransferService.setStateByNumber1(bloodTransfer);
        return Result.success("审核驳回");
    }


    @GetMapping("/normal/addApplication")
    public Result addApplication(BloodTransfer bloodTransfer){
        bloodTransferService.addApplication(bloodTransfer);
        return Result.success("成功");
    }

    @GetMapping("/normal/deleteApplication")
    public Result deleteApplication(BloodTransfer bloodTransfer){
        bloodTransferService.deleteApplication(bloodTransfer);
        return Result.success("成功");
    }


    //验证该申请单号（审核已通过）是否存在，是则返回该申请的所有血液信息
    @GetMapping("/normal/validate")
    public Result validate(String id){

        if(bloodTransferService.isExist(id)){
            return Result.success(bloodTransferService.getAll(id));
        }else{
            return Result.error("500","不存在");
        }

    }

    //根据工号和flag生成单号
    @GetMapping("/normal/getId")
    public Result getId(String id, String flag){
        GenerateNumberBySnowFlake generateNumberBySnowFlake = new GenerateNumberBySnowFlake(Long.valueOf(id),flag);
        String number = generateNumberBySnowFlake.genNumberByflag();
        return Result.success(String.valueOf(number));
    }

}
